/**
 * Usuario - Listagem de usuarios
 */
UsuarioLista = Ext.extend(Bt.grid.GridPanel,{
	
	//Config Options {
	
	 	 title			: 'Lista de Usu&#225;rios'
		,actions		: {
			 visualize			: false
			,disableRemove		: 'eh_admin'
			,disableRemoveTip	: 'Administrador n&#227;o pode ser editado'
		}
		,enableColumn	: {
			 dataIndex	: 'bit_ativo'
			,disable	: 'eh_admin'
			,disableTip	: 'Administrador n&#227;o pode ser editado'
			,directFn	: Server.Usuario.Habilitar
		}	
		
	//}
	
	//Initialize {
	
		,initComponent: function()
		{
			this.store = new Ext.data.DirectStore({
				 idProperty	: 'cd_usuario'
				,autoLoad	: true
				,autoDestroy: true
				,directFn	: Server.Usuario.List
				,fields	: [
					 {name: 'cd_usuario', type: 'int'	}
					,{name: 'ds_login'	, type: 'string'}
					,{name: 'bit_ativo'	, type: 'bool'	}
					,{name: 'ds_nome'	, type: 'string'}
					,{name: 'ds_senha'	, type: 'string'}
					,{name: 'ds_tipo'	, type: 'string'}
					,{name: 'eh_admin'	, type: 'bool' ,convert: function(v,record){ return record.ds_tipo === 'Administrador'; }}
				]
			});
			
			Ext.apply(this,{
				tbar:[{
					 text	: 'Novo Usu&#225;rio'
					,iconCls: 'ico_add'
					,handler: this._onBtnAddClick
					,scope	: this
				}]
				,columns: [
					 {header: 'Nr.'		, dataIndex: 'cd_usuario'	, width: 60, align:'center'} 
					,{header: 'Nome'	, dataIndex: 'ds_nome'} 
					,{header: 'Login'	, dataIndex: 'ds_login'}
					,{header: 'Tipo'	, dataIndex: 'ds_tipo' , renderer: this._tipoUsuarioRenderer}
				]
			});
				
			UsuarioLista.superclass.initComponent.call(this);
		}	
		
	//}
	
	//Override {
		
		,destroy: function()
		{
			if (this.winCadastro) {
				this.winCadastro.destroy();
			}
				
			UsuarioLista.superclass.destroy.call(this);
		}
		
		,onEditClick: function(me, record, rowIndex)
		{
			this._openWindowCadastro(Ext.apply({},record.data));
		}
		
		,onRemoveClick: function(me, record, rowIndex)
		{
			Ext.Msg.confirm('Confirma&#231;&#227;o','Deseja excluir este registro?',function(opt)
			{
				if(opt === 'no'){
					return false;
				}
				
 				Server.Usuario.Delete(record.id,function(result)
				{
					if(result && result.success)
					{
						record.store.remove(record);
					}
				});
			},this);
		}
		
	//}
	
	//Listeners{
				
		,_onBtnAddClick: function()
		{
			this._openWindowCadastro();
		}
		
	//}
	
	//Demais metodos{
		
		,_tipoUsuarioRenderer: function(v)
		{
			return 	v === 'AnalistaQualidade' ? 'Analista de Qualidade' :
					v === 'AnalistaSistemas' ? 'Analista de Sistemas' :
					v === 'GerenteProjetos' ? 'Gerente de Projetos' : v;					
		}
		
		,_openWindowCadastro: function( data )
		{
			if(!this.winCadastro)
			{
				this.winCadastro = new UsuarioCadastro(
				{
					 closeAction: 'hide'
					,hideMode	: 'offsets'
					,listeners	: {
						 scope		: this
						,confirmar	: function()
						{
							this.store.reload();
						}
					}
				});
			}
			
			//show
			this.winCadastro.show();
			
			if(data)
			{
				this.winCadastro.loadData(data);
			}
			else
			{
				this.winCadastro.reset(data);
			}
		}
	
	//}
	
});

Ext.reg('tst-usuariolista',UsuarioLista);

/**
 * Usuario - Cadastro de usuarios
 */
UsuarioCadastro = Ext.extend(Ext.Window,{
	
	//Config Option {
	
		 title		: 'Cadastro de Usu&#225;rio'
		,iconCls	: 'user_add'
		,layout		: 'fit'
		,constrain	: true
		,modal		: true
		,width		: 500
		,height		: 533
	
	//}
		
	//Initialize {
		
		,constructor: function()
		{
			this.addEvents('confirmar');
			
			UsuarioCadastro.superclass.constructor.apply(this,arguments);
		}
		
		,initComponent: function()
		{
			//renderTo e size{
			
				this.renderTo 	= this.renderTo||Ext.getBody();
				this.width		= Math.floor(this.renderTo.getWidth() *0.9).constrain(this.minWidth ,this.width );
				this.height		= Math.floor(this.renderTo.getHeight()*0.9).constrain(this.minHeight,this.height);
				
			//}	
			
			this.form = new Ext.form.FormPanel({
			 	 xtype			: 'form'
				,bodyStyle		: 'padding:10px;'
				,defaultType	: 'textfield'
				,buttonAlign	: 'right'
				,labelAlign		: 'top'
				,layoutConfig	:{ trackLabels: true }
				,border			: false
				,autoScroll		: true
				,api			: {
    				submit: Server.Usuario.Create
				}
				,defaults	: {
					anchor:'-17'
				}
				,items		: [{
					 xtype		: 'hidden'
					,name		: 'cd_usuario'
				},{
					 fieldLabel	: 'Nome'
					,name		: 'ds_nome'
					,itemId		: 'ds_nome'
					,ref		: '../txtNome'
					,allowBlank	: false
					,maxLength	: 200
				},{
				 	 xtype		: 'container'
					,layout		: 'column'
					,defaults	: {
						 xtype			: 'container'
						,layout			: 'form'
						,labelAlign		: 'top'
						,defaultType	: 'textfield'
						,columnWidth	: 0.5
						,layoutConfig	:{ trackLabels: true }
					}
					,items		: [{
						 style: 'padding-right:2px;'
						,items: {
						 	 fieldLabel	: 'Login'
							,name		: 'ds_login'
							,anchor		: '0'
							,allowBlank	: false					
							,maxLength	: 30
						}
					},{
						 style: 'padding-left:2px;'
						,items: {
							 fieldLabel	: 'Senha'
							,name		: 'ds_senha'					 
							,inputType	: 'password'
							,anchor		: '0'
							,allowBlank	: false
							,maxLength	: 10
							,minLength	: 4
						}
					}]
				},{
					 xtype			: 'checkbox'
					,boxLabel		: 'Ativo'
					,name			: 'bit_ativo'
					,ref			: '../chbAtivo'
					,inputValue		: 1
					,checked		: true
					,hideLabel		: true
				},{
					 xtype		: 'fieldset'
					,title		: 'Par&#226;metros'
					,defaultType: 'checkbox'
					,labelAlign	: 'left'
					,itemId		: 'fieldParametros'
					,defaults	: {
						 inputValue	: 1
						,hideLabel	: true
						,checked	: true
					}
					,items		: [{
						 boxLabel	: 'Cadastra ambientes'
						,name		: 'bit_cad_ambiente'
					},{
						 boxLabel	: 'Cadastra casos de teste'
						,name		: 'bit_cad_cas_teste'
					},{
						 boxLabel	: 'Cadastra planos de execu&#231;&#227;o'
						,name		: 'bit_cad_pl_execucao'
					},{
						 boxLabel	: 'Cadastra planos de teste'
						,name		: 'bit_cad_pl_teste'
					},{
						 boxLabel	: 'Cadastra projetos'
						,name		: 'bit_cad_projeto'
					},{
						 boxLabel	: 'Cadastra tipos de erros'
						,name		: 'bit_cad_tp_erro'
					},{
						 boxLabel	: 'Visualiza dashboard'
						,name		: 'bit_vis_dash'
					},{
						 boxLabel	: 'Visualiza relat&#243;rios'
						,name		: 'bit_vis_rel'
					}]
				}]
			});
			
			Ext.apply(this,{
				 items	: this.form
				,buttons: [{
					 text	: 'Confirmar'
					,iconCls: 'ico_accept'
					,scope	: this
					,handler: this._onBtnConfirmarClick
				},{
					 text	: 'Cancelar'
					,iconCls: 'ico_cancel'
					,scope	: this
					,handler: function(){ this.hide(); }
				}]
			});
			
			UsuarioCadastro.superclass.initComponent.call(this);
		}	
		
	//}
	
	//Listeners{
	
		,_onBtnConfirmarClick: function()
		{
			this.form.getForm().submit({
				 waitMsg	: 'Enviando formul&#225;rio...'
				,waitTitle	: 'Aguarde'
				,scope		: this
				,success	: function()
				{
					this.hide();
					this.fireEvent('confirmar');
				}
			});
		}
		
	//}
	
	//Demais métodos {
	
		,loadData: function(data)
		{
			this.form.getForm().setValues(data);
			
			var txtSenha = this.form.getForm().findField('ds_senha');
			txtSenha.allowBlank = true;
			txtSenha.reset();
			
			this.setIconClass('ico_user');
			this.setTitle('Edi&#231;&#227;o de Usu&#225;rio');
			this.chbAtivo.show();
			this.chbAtivo.setValue(1);
			this.txtNome.focus.defer(500,this.txtNome);
			
			if(data.eh_admin)
			{
				this.form.form.findField('ds_login').hide();
				this.chbAtivo.hide();	
				this.form.getComponent('fieldParametros').hide();
			}
			
		}
		
		,reset: function()
		{
			this.form.getForm().reset();
			
			this.setIconClass('ico_user_add');
			this.setTitle('Cadastro de Usu&#225;rio');
			this.chbAtivo.hide();
			this.txtNome.focus.defer(500,this.txtNome);
			
			this.form.form.findField('ds_login').show();
			this.form.getComponent('fieldParametros').show();
		}
		
	//}
	
});